//
//  QuickLoginViewController.m
//  MacWeb
//
//  Created by 小贝 on 16/3/16.
//  Copyright © 2016年 Boye. All rights reserved.
//

#import "QuickLoginViewController.h"
#import "UserLoginViewController.h"
#import "HHLoginViewController.h"
@interface QuickLoginViewController ()<NSTextFieldDelegate>
//属性
@property (weak) IBOutlet NSTextField *phoneNumberTF;//账号
@property (weak) IBOutlet NSButton *forgetBtn;//忘记密码
@property (weak) IBOutlet NSButton *commonLogin;//普通登录
@property (weak) IBOutlet NSButton *loginBtn;//登录
//事件
- (IBAction)nextAction:(id)sender;//点击登录
- (IBAction)forgetAction:(id)sender;//点击忘记密码
- (IBAction)commonAction:(NSButton *)sender;//点击普通登录
@end
@implementation QuickLoginViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do view setup here.
    [NSButton setTextColor:self.forgetBtn andColor:[NSColor whiteColor]];
    self.phoneNumberTF.delegate = self;
    [self changeCursor];
}

//点击登录按钮
- (IBAction)nextAction:(id)sender {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.margin = 10.f;
    hud.color = [NSColor clearColor];
    hud.xOffset = self.phoneNumberTF.bounds.size.width;
    hud.removeFromSuperViewOnHide = YES;
    [hud hide:YES afterDelay:2];
    if ([NSString isStringNull:self.phoneNumberTF.stringValue]) {
        hud.labelText = @"请填入手机号或卡号";
        return ;
    }

    NSString *url = [NSString stringWithFormat:@"%@?number=%@&bookId=0&date=20160101",QuickLoginPath,self.phoneNumberTF.stringValue];
    NSString *requesturl = [NSString URLEncodedString:url];
    [HHNetRequest requestWithGet:requesturl parmeter:nil successBlock:^(id resultValue) {
        NSLog(@"%@",resultValue);
        NSString *result = [resultValue objectForKey:@"loginRst"];
      
            if ([result isEqualToString:@"0"]) {
//            hud.labelText = @"登录成功";
            ViewController *mainVC = [self.storyboard instantiateControllerWithIdentifier:@"MainVC"];
            mainVC.userName = self.phoneNumberTF.stringValue;
            mainVC.type = @"quick0";
            [[NSApp mainWindow] setContentViewController:mainVC];
            float width = [NSScreen mainScreen].frame.size.width;
            float height =[NSScreen mainScreen].frame.size.height;
            [[NSApp mainWindow] setContentSize:NSMakeSize(width, height)];
            
        }else if ([result isEqualToString:@"1"]){
            NSArray *userArray = [resultValue objectForKey:@"users"];
            UserLoginViewController *userVC = [self.storyboard instantiateControllerWithIdentifier:@"userVC"];
            userVC.userArr = userArray;
            userVC.userName = self.phoneNumberTF.stringValue;
            [[NSApp mainWindow] setContentViewController:userVC];
            float width = [NSScreen mainScreen].frame.size.width;
            float height =[NSScreen mainScreen].frame.size.height;
            [[NSApp mainWindow] setContentSize:NSMakeSize(width, height)];
        
        }else{
            hud.labelText = @"没有符合的用户";
        }
    } failBlock:^(id failedBlock) {
        
    }];
    
}
//点击忘记密码
- (IBAction)forgetAction:(id)sender {
    ViewController *vc = [self.storyboard instantiateControllerWithIdentifier:@"MainVC"];
    vc.type = @"forget";
    [self setContentVC:vc];
}
//点击普通登录
- (IBAction)commonAction:(NSButton *)sender {
    HHLoginViewController *loginVC = [self.storyboard instantiateControllerWithIdentifier:@"loginVC"];
    [self setContentVC:loginVC];
}
//更换视图
- (void)setContentVC:(NSViewController *) vc{
    [[NSApp mainWindow] setContentViewController:vc];
    float width = [NSScreen mainScreen].frame.size.width;
    float height =[NSScreen mainScreen].frame.size.height;
    [[NSApp mainWindow] setContentSize:NSMakeSize(width, height)];
}
#pragma mark --设置光标
- (void)changeCursor{
    [self.forgetBtn addTrackingRect:[self.forgetBtn bounds] owner:self userData:nil assumeInside:NO];
    [self.commonLogin addTrackingRect:[self.commonLogin bounds] owner:self userData:nil assumeInside:NO];
    [self.loginBtn addTrackingRect:[self.loginBtn bounds] owner:self userData:nil assumeInside:NO];
}
-(void)mouseEntered:(NSEvent *)theEvent{
    NSCursor *cursor = [NSCursor pointingHandCursor];
    [cursor set];
}
- (void)mouseExited:(NSEvent *)theEvent{
    NSCursor *cursor = [NSCursor arrowCursor];
    [cursor set];
}
@end
